<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Input;
use App\News;


class NewsController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */

     // 列表页
    public function News()
    {

       $news = News::orderBy('id','desc')->paginate(10);    // 分页，每页显示 2 条

        return view('news',[
            'newss' => $news
        ]);
    }



    // 添加页面 & 提交方法
    public function create(Request $request){
        // 提交到当前方法
        if($request->isMethod('POST')){
            $validator = \Validator::make($request->input(), [
                'News.title' => 'required|min:2|max:20',
                'News.author' => 'required',
                'News.content' => 'required'
            ], [
                'required'  => ':attribute 为必填项',
                'min' => ':attribute 最少为两个字符',
                'max' => ':attribute 最多为二十个字符'
                
            ], [
                'News.title' => '标题',
                'News.author' => '作者',
                'News.content' => '内容'
            ]);

            if($validator->fails()){
                // withErrors()：手动注册错误信息，把 $validator 对象传入
                return redirect()->back()->withErrors($validator)->withInput();
            }


            $data = $request->input('News');

            if(News::insert([
                'title' => $data['title'],
                'author' => $data['author'],
                'content' => $data['content'],
                'created_at' =>date('Y-m-d:H:i:s',time()),
                'updated_at' =>date('Y-m-d:H:i:s',time()),
            ])){
                return redirect('news')->with('success', '添加成功');	// 闪存信息
            }else{
                return redirect()->back();
            }

            //$db = DB::table("news");
            //$res = $db -> insert([
                //'title' => $data['title'],
                //'author' => $data['author'],
                //'content' => $data['content'],
                //'created_at' =>date('Y-m-d:H:i:s',time()),
                //'updated_at' =>date('Y-m-d:H:i:s',time()),
            //]);
            //if(var_dump($res)){
                //return redirect('news')->with('success', '添加成功');
            //}else{
                //return redirect()->back();
            //}
            
        }

        return view('create');

    }

    
    // 提交到指定方法：action="{{ url('news/save') }}"
    // 保存添加
	public function save(Request $request){
        $data = $request->input('News');

        // 获取模型
        $news = new News();
        $news->title = $data['title'];
        $news->author = $data['author'];
        $news->content = $data['content'];

       

        if($news->save()){
            return redirect('news');
        }else{
            return redirect()->back();
        }
		
    } 

    // 修改
public function update(Request $request, $id){
    $news = News::find($id);

    if($request->isMethod('POST')){
        $this->validate($request, [
            'News.title' => 'required|min:2|max:20',
            'News.author' => 'required',
            'News.content' => 'required'
        ], [
            'required'  => ':attribute 为必填项',
            'min' => ':attribute 最少为两个字符',
            'max' => ':attribute 最多为二十个字符'

        ], [
            'News.title' => '标题',
            'News.author' => '作者',
            'News.content' => '内容'
        ]); 

        $data = $request->input('News');
        $news->title = $data['title'];
        $news->author = $data['author'];
        $news->content = $data['content'];

        if(News::where('id',$id)-> update([
            'title' => $data['title'],
            'author' => $data['author'],
            'content' => $data['content'],
            'updated_at' =>date('Y-m-d:H:i:s',time()),
        ])){
            return redirect('news')->with('success', '修改成功 - '. $id);
        }
    }

    return view('update', [
        'news' => $news
    ]);
}

// 详情
public function detail($id){
    $news = News::find($id);

    return view('detail', [
        'news' => $news
    ]);
}


// 删除
public function delete($id){
    $news = News::find($id);

    if($news->delete()){
        return redirect('news')->with('success', '删除成功'. $id);
    }else{
        return redirect('news')->with('error', '删除失败'. $id);
    }
}


}